<template>
	<view class="container">
		<scroll-view scroll-y class="content" @scrolltolower="scrollBottom">
			<view class="title font_title">历史订单</view>
			<view class="time_box flex_between" @tap="openDatePopup" @popupConfirm="handleDateConfirm">
				<view class="time_item flex_center">
					<text>{{date.before}}</text>
					<u-icon name="arrow-down" size="12" style="margin-left:10rpx"></u-icon>
				</view>
				<view class="time_item flex_center">
					<text>{{date.after}}</text>
					<u-icon name="arrow-down" size="12" style="margin-left:10rpx"></u-icon>
				</view>
			</view>
			<view class="balance col_center">
				<view class="balance_title">盈亏</view>
				<view class="balance_detail" :class="allMoney>0?'up':'down'">
					{{allMoney}}
				</view>
			</view>
			<view class="list_box">
				<view class="list" v-if="1==1">
					<view class="item" v-for="item in list" @tap="openPopup(item.id)">
						<view class="header flex_between">
							<view class="symbol font_title">{{item.pro_name}}</view>
							<view class="status">{{item.limit_type_str}}</view>
						</view>
						<view class="price flex_between">
							<view class="price_change flex">
								<text class="old_price">{{item.price}}</text>
								<u-icon name="arrow-rightward" size="12" style="margin: 0 10rpx;" color="#000"
									bold></u-icon>
								<text class="new_price"
									:class="item.type_str == '买入' ?'up':'down'">{{item.saleprice}}</text>
							</view>
							<view class="amount flex" :class="item.type_str == '买入' ?'border_up':'border_down'">
								<view class="amount_title" :class="item.type_str == '买入' ?'bg_up':'bg_down'">
									{{item.type_str}}
								</view>
								<view class="amount_detail" :class="item.type_str == '买入' ?'up':'down'">{{item.num}} 手数
								</view>
							</view>
						</view>
						<view class="detail flex_between">
							<view class="detail_list">
								<view class="detail_item">保证金 {{item.credit}}</view>
								<view class="detail_item">订单ID {{item.order_sn}}</view>
								<view class="detail_item">{{item.createtime}}</view>
							</view>
							<view class="detail_price" :class="item.type_str == '买入'?'up':'down'">
								{{item.money}}
							</view>
						</view>
					</view>
				</view>
				<view class="no_data flex_center" v-else>
					<image src="../../static/img/empty.png" style="width: 420rpx;height:380rpx;margin-top: 30rpx;" />
				</view>
			</view>
		</scroll-view>
		<date-popup :show="dateShow" @popupClose="popupClose" @popupConfirm="popupConfirm"></date-popup>
		<u-popup :show="detailShow" @close="detailClose">
			<view class="mask">
				<view class="mask_close flex_center" @tap="detailClose">
					<uni-icons type="closeempty"></uni-icons>
				</view>
				<view class="mask_title font_title">{{"订单详情"}}</view>
				<view class="mask_content font_title">
					<view class="header flex_between">
						<view class="symbol">{{form.pro_name}}</view>
						<view class="status">{{form.limit_type_str}}</view>
					</view>
					<view class="price flex_between">
						<view class="price_change flex">
							<text class="old_price">{{form.price}}</text>
							<u-icon name="arrow-rightward" size="12" style="margin: 0 10rpx;" color="#000"
								bold></u-icon>
							<text class="new_price"
								:class="form.type_str == '买入' ?'up':'down'">{{form.saleprice}}</text>
						</view>
						<view class="amount flex" :class="form.type_str == '买入'?'border_up':'border_down'">
							<view class="amount_title" :class="form.type_str == '买入'?'bg_up':'bg_down'">
								{{form.type_str}}
							</view>
							<view class="amount_detail" :class="form.type_str == '买入'?'up':'down'">{{form.num}} 手数
							</view>
						</view>
					</view>
					<view class="detail flex_between">
						<view class="detail_item">
							<view class="item_title">止损</view>
							<view class="item_data">{{form.s_l}}</view>
						</view>
						<view class="detail_item">
							<view class="item_title">止盈</view>
							<view class="item_data">{{form.t_p}}</view>
						</view>
						<view class="detail_item">
							<view class="item_title">盈亏</view>
							<view class="item_data">{{form.money}}</view>
						</view>
						<view class="detail_item">
							<view class="item_title">手续费</view>
							<view class="item_data">{{form.commission}}</view>
						</view>
					</view>
					<view class="info flex_between">
						<view class="info_list">
							<view class="info_item">保证金 {{form.credit}}</view>
							<view class="info_item">订单ID {{form.order_sn}}</view>
							<view class="info_item">下单时间 {{form.createtime}}</view>
							<view class="info_item">平仓时间 {{form.sale_time}}</view>
						</view>
						<view class="info_price" :class="form.sj_money>0?'up':'down'">
							{{form.sj_money}}
						</view>
					</view>
				</view>
			</view>
		</u-popup>
		<tabbarView id="4"></tabbarView>
	</view>
</template>

<script>
	import tabbarView from "@/components/tabbar.vue"
	import datePopup from "./components/datePopup.vue"
	import {
		stockOrderListApi,
		orderDetailApi
	} from '@/api/api.js'
	export default {
		components: {
			tabbarView,
			datePopup
		},
		data() {
			return {
				page: 1,
				form: {},
				status: '2',
				allMoney: '',
				list: [],
				dateShow: false,
				detailShow: false,
				date: {},
				timer: null
			};
		},
		onShow() {
			let time = new Date().getTime()
			this.date.before = uni.$u.timeFormat(time, 'yyyy-mm') + "-01";
			this.date.after = uni.$u.timeFormat(time, 'yyyy-mm-dd');
			this.getStockOrder()
		},
		methods: {
			// 获取订单详情
			getOrderDetail(options) {
				const token = uni.getStorageSync('token')
				const id = options
				orderDetailApi({
					token,
					id
				}).then(res => {
					this.form = res.data.data
					console.log('from:', this.form)
				})
			},
			// 获取订单
			getStockOrder() {
				const token = uni.getStorageSync('token')
				const status = this.status
				const starttime = this.date.before
				const endtime = this.date.after
				const page = this.page
				stockOrderListApi({
					page,
					token,
					status,
					starttime,
					endtime
				}).then(res => {
					this.allMoney = res.data.data.all_money
					if (this.page == 1) {
						this.list = res.data.data.data
					} else {
						this.list = [...this.list, ...res.data.data.data]
					}
				})
			},
			openDatePopup() {
				this.dateShow = true
			},
			popupClose() {
				this.dateShow = false
			},
			popupConfirm(opt) {
				console.log("确认时间", opt)
				this.date = opt
				this.getStockOrder();
				this.popupClose()
			},
			// 订单详情
			openPopup(opt) {
				this.detailShow = true
				this.getOrderDetail(opt)
			},
			detailClose() {
				this.detailShow = false
			},
			scrollBottom() {
				console.log("滚动到底部")
				this.page++
				this.getStockOrder()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.content {
			.title {
				font-size: 60rpx;
			}

			.time_box {
				height: 80rpx;
				line-height: 80rpx;
				background-color: rgba(237, 237, 237, .46);
				margin-top: 40rpx;
				border-radius: 40rpx;
				overflow: hidden;

				.time_item {
					flex: 1;
					font-family: PM;
					line-height: 80rpx;
				}
			}

			.balance {
				margin-top: 40rpx;

				.balance_title {
					opacity: .5;
					font-size: 24rpx;
				}

				.balance_detail {
					font-size: 60rpx;
					margin-top: 10rpx;
					font-family: PM;
				}
			}

			.list_box {
				margin-top: 40rpx;

				.list {
					.item {
						border-bottom: 2rpx solid rgba(237, 237, 237, 0.46);
						padding: 30rpx 0;

						.header {
							.symbol {
								font-size: 32rpx;
							}

							.status {
								font-size: 24rpx;
								opacity: .5;
							}
						}

						.price {
							.amount {
								font-size: 20rpx;
								border-radius: 10rpx;
								margin-top: 6rpx;

								.amount_title {
									padding: 4rpx 14rpx;
								}

								.amount_detail {
									padding: 4rpx 10rpx;
								}
							}
						}

						.detail {
							margin-top: 20rpx;

							.detail_list {
								opacity: .5;
								font-size: 20rpx;
							}

							.detail_price {
								font-size: 40rpx;
							}
						}
					}
				}
			}
		}

		.mask {
			padding: 40rpx;
			background: #fff;
			border-radius: 40rpx 40rpx 0px 0px;
			overflow: hidden;
			transition: all 0.25s linear 0s;

			.mask_close {
				width: 72rpx;
				height: 72rpx;
				background-color: hsla(0, 0%, 92.9%, .46);
				border-radius: 20rpx;
			}

			.mask_title {
				padding-top: 40rpx;
				font-size: 50rpx;
			}

			.mask_content {
				margin-top: 40rpx;
				padding-bottom: 30rpx;

				.header {
					.symbol {
						font-size: 36rpx;
					}

					.status {
						font-size: 28rpx;
						opacity: .5;
					}
				}

				.price {
					.amount {
						font-size: 20rpx;
						border-radius: 10rpx;
						margin-top: 6rpx;

						.amount_title {
							padding: 4rpx 14rpx;
						}

						.amount_detail {
							padding: 4rpx 10rpx;
						}
					}
				}

				.detail {
					box-shadow: 0px 3px 8px 0 rgba(0, 0, 0, .16);
					text-align: center;
					margin-top: 30rpx;
					border-radius: 20rpx;
					padding: 20rpx 0;

					.detail_item {
						flex: 1;

						.item_title {
							opacity: .5;
							font-size: 24rpx;
						}

						.item_data {
							font-size: 32rpx;
							margin-top: 10rpx;
						}
					}
				}

				.info {
					margin-top: 30rpx;

					.info_list {
						opacity: .5;
						font-size: 24rpx;
						line-height: 34rpx;
					}

					.info_price {
						font-size: 40rpx;
					}

				}
			}
		}
	}
</style>